package com.qbw.core.base;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.graphics.drawable.BitmapDrawable;
import androidx.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.PopupWindow;

/**
 * @author qinbaowei
 * @date 2017/10/23
 * @email qbaowei@qq.com
 */

public abstract class BasePopupWindow<B extends ViewDataBinding> extends PopupWindow implements PopupWindow.OnDismissListener {
    protected Context mContext;
    protected B mBinding;

    public BasePopupWindow(Context context, @LayoutRes int layoutId) {
        this(context,
             layoutId,
             ViewGroup.LayoutParams.MATCH_PARENT,
             ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public BasePopupWindow(Context context, @LayoutRes int layoutId, int width, int height) {
        this(context,
             DataBindingUtil.inflate(LayoutInflater.from(context), layoutId, null, false),
             width,
             height);
    }

    public BasePopupWindow(Context context, ViewDataBinding binding, int width, int height) {
        super(binding.getRoot(), width, height);
        mContext = context;
        mBinding = (B) binding;
        init();
    }

    protected void init() {
        setTouchable(enableDismissWhenOutside());
        setOutsideTouchable(enableDismissWhenOutside());
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setOnDismissListener(this);
    }

    @Override
    public void onDismiss() {
        //Util.toogleKeybord(mContext, false);
    }

    protected boolean enableDismissWhenOutside() {
        return true;
    }
}
